package com.rose.doorto.footbathdoor.controler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rose.doorto.footbathdoor.mapper.ProjectMapper;
import com.rose.doorto.footbathdoor.pojo.Project;
import com.rose.doorto.footbathdoor.pojo.Teacher;
import com.rose.doorto.footbathdoor.service.IProjectService;
import com.rose.doorto.footbathdoor.service.TeacherService;
import com.rose.doorto.footbathdoor.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <P>
 *     服务项目表  前端控制器
 * </P>
 * @Author 李庆超
 * @email 1481791489@qq.com
 * @Package：com.rose.doorto.footbathdoor.controler
 * @Project：door-to-door
 * @name：ProjectController
 * @date 2024/07/10 2024/7/10:10:51:
 */
@Controller
@RequestMapping("/doorto/project")
public class ProjectController {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private IProjectService service;

    @Autowired
    private TeacherService teacherService;

    /**
     * 项目上架
     * @param project
     * @return
     */
    @PostMapping("/insertProject")
    public Result insertProject(Project project){
        boolean save = service.save(project);
        if (save){
            return Result.success("data",200,"新增成功");
        }else {
            return Result.error(500,"操作失败");
        }
    }
    /**
     * 获取所有项目信息
     * @return
     */
    @GetMapping("/getProject")
    public String getProject(Model model){
        List<Project> list = projectMapper.selectList(null);
        model.addAttribute("projects",list);
        return "index.html";
    }

    /**
     * 查看项目详情
     * @param id
     * @return
     */
    @GetMapping("/getProjectById")
    public Result getProjectById(int id){
        Result projectById = service.getProjectById(id);
        return projectById;
    }

    @GetMapping("/getProjectById2")
    public List<Project> getProjectById2(int id){
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        List<Project> list = service.list(wrapper);
        return list;
    }

    /**
     * 根据项目id获取技师列表
     * @param id
     * @return
     */
//    @RequestMapping(value = "/getTeacherByProjectId", method = RequestMethod.GET)
    @GetMapping( "/getTeacherByProjectId/{id}")
    public String getTeacherByProjectId(@PathVariable("id") int id, Model model){
        List<Teacher> teacher = teacherService.getTeacherByProjectId(id);
        if (teacher!=null){
            model.addAttribute("list",teacher);
            System.out.println(teacher+"这是打印的数据======================================================================");
            return "teacherlist.html";
        }else {
            return "";
        }
    }



}
